package com.bite.blog.controller;

import com.bite.blog.Utils.JwtUtils;
import com.bite.blog.constants.Constant;
import com.bite.blog.model.BlogInfo;
import com.bite.blog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;
    @RequestMapping("/getList")
    public List<BlogInfo> queryBlogList(){
        return blogService.queryBlogList();
    }
    @RequestMapping("/getBlogDetail")
    public BlogInfo queryBlogDetail(Integer blogId,HttpServletRequest request){
        BlogInfo blogInfo = blogService.queryBlogDetail(blogId);
        String user_token = request.getHeader(Constant.USER_TOKEN_HEADER);
        Integer userId = JwtUtils.getUserIdFromToken(user_token);
        if(userId!=null&&userId==blogInfo.getUserId()){
            blogInfo.setIsLoginUser(true);
        }
        return blogInfo;
    }
    @RequestMapping("/add")
    public boolean publishBlog(String title, String content, HttpServletRequest request){
        log.info("publishBlog,接收参数:title:{},content:{}",title,content);
        if(!StringUtils.hasLength(title)||
                !StringUtils.hasLength(content)){
            log.error("title or content 为空");
            return false;
        }
        String user_token = request.getHeader(Constant.USER_TOKEN_HEADER);
        Integer userId = JwtUtils.getUserIdFromToken(user_token);
        if(userId==null||userId<=0){
            return false;
        }
        BlogInfo blogInfo = new BlogInfo(title,content,userId);
        Integer result = blogService.publishBlog(blogInfo);
        if(result<1){
            log.error("博客发布失败");
            return false;
        }
        return true;
    }
    @RequestMapping("/update")
    public Boolean update(Integer blogId,String title,String content){
        if(blogId==null||!StringUtils.hasLength(title)||
           !StringUtils.hasLength(content)){
            log.error("ID/标题/内容不合法");
            log.info("blogId"+blogId);
            log.info(title);
            log.info(content);
            return false;
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setTitle(title);
        blogInfo.setContent(content);
        Integer result =   blogService.updateBlog(blogInfo);
        if(result<1){
            return  false;
        }
        return true;
    }
    @RequestMapping("/delete")
    public Boolean delete(Integer blogId){
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        Integer result =   blogService.updateBlog(blogInfo);
        if(result<1){
            return  false;
        }
        return true;
    }
}